﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CjaxExam.Models.Interfaces;
using System.Collections.Generic;
using CjaxExam.Models.DataBase;

namespace CjaxExam.Models.Entities
{
    public class ForExam:IForPublish,IForPE,IForPaper//,IForPublish1,IForPublish2,IForPublish3

    {

        public IList<ex_paper> ListP()
        {
            ForExamDataContext exam = new ForExamDataContext();
            var getPaperInfo = from p in exam.ex_paper
                               select p;
            return getPaperInfo.ToList();

        }
        public IList<ex_paper_exam> ListPE()
        {
            ForExamDataContext exam = new ForExamDataContext();
            var getPEInfo = from pe in exam.ex_paper_exam
                            select pe;
            return getPEInfo.ToList();
        }
        public IList<ex_publish> ListE()
        {
            ForExamDataContext exam = new ForExamDataContext();
            
            var getExamInfo = from e in exam.ex_publish
                              select e;
            
            return getExamInfo.ToList();
        }
        public IList<GetKgByExamResult> ListKg(string id)
        {
            //考卷客观题
            ForExamDataContext exam = new ForExamDataContext();
            var kg = exam.GetKgByExam(id);
            return kg.ToList();
        }
        public IList<GetZgByExamResult> ListZg(string id)
        {
            //考卷主观题
            ForExamDataContext exam = new ForExamDataContext();
            var zg = exam.GetZgByExam(id);
            return zg.ToList();
        }
        public IList<ex_publish> GetExamInfo(string id)
        {
            //获取考卷的基本信息
            ForExamDataContext exam = new ForExamDataContext();
            var gei = from e in exam.ex_publish
                      where e.publish_id == id
                      select e;
            return gei.ToList();
        }
        public string  GetPaperIDByExam(string id)
        {
            ForExamDataContext paper = new ForExamDataContext();
            string backid = "";
            var pa = from e in paper.ex_paper_exam
                    where e.publish_id == id
                    select e;
            foreach (var m in pa)
            {
                backid = m.paper_id;
            }
            return backid;
        }
        public List<ex_paper> GetPaperByExam(string id)
        {
            ForExamDataContext paper = new ForExamDataContext();
            var pa = from e in paper.ex_paper
                     where e.paper_id==id
                     select e;
            return pa.ToList();
        }
        public IList<GetMarkByExamResult> GetMark(string id)
        { 
        //获取试卷卷面分、专业等信息
            ForExamDataContext exam = new ForExamDataContext();
            var gm = exam.GetMarkByExam(id);
            return gm.ToList();
        }
        public string GetId(int no)
        {
            string backid ="";
            //no为获取资源ID的数字标识
            ForExamDataContext exam = new ForExamDataContext();
            exam.ForId(no, ref backid);
            return backid;
        }
        public void InsertExam(ExamVO e,string[] uid)
        { //插入考试表
            ForExamDataContext insert = new ForExamDataContext();
            var publish = new ex_publish
            {
                publish_id = e.Pid,
                publish_name = e.Pname,
                publish_date = e.Pdate,
                publish_test_time = e.Ptt,
                publish_examer_expect = e.Pee,
                publish_exam_type = e.Pet,
                publish_endif = "否",
                publish_endall = "否",
                publish_examer_reality = null,
                publish_pass = null
            };
            insert.ex_publish.InsertOnSubmit(publish);
            

            var ep = new ex_paper_exam
            {
                paper_id = e.Paperid,
                publish_id = e.Pid,
                pe_taxis = 0
            };
            insert.ex_paper_exam.InsertOnSubmit(ep);
            insert.SubmitChanges();//提交一次

            int i;
            for (i = 0; i < uid.Length - 1; i++)//一次考试关联多个考生
            {
                var eu = new ex_record
                {
                    record_no = GetId(6),//获取考试、用户关联表的id流水号
                    publish_id = e.Pid,
                    user_userid = uid[i] //循环获取关联的考生ID
                };
                insert.ex_record.InsertOnSubmit(eu);
                insert.SubmitChanges();//循环的提交
                //插入考试、用户关联表
            }
            
            
        }
        //public void InsertExamUser(ExamVO e)
        //{
        //    ForExamDataContext insert = new ForExamDataContext();
        //    var eu = new ex_record
        //    {
        //        record_no=e.ExamUserid,
        //        publish_id = e.Pid,
        //         user_userid=e.Userid
        //    };
        //    insert.ex_record.InsertOnSubmit(eu);
        //    insert.SubmitChanges();
        //}
        //public void InsertExamPaper(ExamVO e)
        //{
        //    ForExamDataContext insert = new ForExamDataContext();
        //    var ep = new ex_paper_exam
        //    {
        //        paper_id=e.Paperid,
        //        publish_id = e.Pid,
        //        pe_taxis = 0  
        //    };
        //    insert.SubmitChanges();
        //}
        public void UpdateExam(ExamVO e)
        {
            ForExamDataContext update = new ForExamDataContext();
            var ue = from exam in update.ex_publish
                     where exam.publish_id == e.Pid
                     select exam;
            foreach (var m in ue)
            {
                m.publish_name = e.Pname;
                m.publish_exam_type = e.Pet;
                m.publish_test_time = e.Ptt;
                m.publish_date = e.Pdate;
            }
            update.SubmitChanges();
        }
        public void UpdatePaper(ExamVO e, string oldid)
        {
            ForExamDataContext update = new ForExamDataContext();
            var pa = from p in update.ex_paper_exam
                     where p.paper_id == oldid &&p.publish_id==e.Pid 
                     select p;
            foreach (var m in pa)
            {
                m.paper_id = e.Paperid;
            }
            update.SubmitChanges();


        }
        public IList<ex_user> GetUserNameBySecond(string second) {
            ForExamDataContext getuser = new ForExamDataContext();
            var username = from u in getuser.ex_user
                           where u.user_secondpart == second
                           select u;
            return username.ToList();
        }
        public IList<ex_user> GetUserNameByThird(string third) {
            ForExamDataContext getuser = new ForExamDataContext();
            var username = from u in getuser.ex_user
                           where u.user_department == third
                           select u;
            return username.ToList();
        }
        public IList<ex_user> GetUserNameAll()
        {
            ForExamDataContext getuser = new ForExamDataContext();
            var username = from u in getuser.ex_user
                           select u;
            return username.ToList();
        }
        public IList<ex_paper> GetPaper()
        {
            ForExamDataContext allpaper = new ForExamDataContext();
            var paper = from p in allpaper.ex_paper
                        select p;
            return paper.ToList();
        }

        //public IList<ex_publish1> ListE2()
        //{

        //    ForExamDataContext exam = new ForExamDataContext();
        //    var getExamEnd = from e in exam.ex_publish
        //                      where e.publish_endall == "Y"
        //                      select e;
        //    return getExamEnd.ToList();
        //}
        //public IList<ex_publish> ListE2()
        //{// 完结试卷

        //    ForExamDataContext exam = new ForExamDataContext();
        //    exam.list1 = from e in exam.ex_publish
        //                 where e.publish_endall == "Y"
        //                 select e;
        //    var getExamInfo = from e in exam.ex_publish where e.publish_endall=="Y"
        //                      select e;
        //    return getExamInfo.ToList();
        //}
        //public IList<ex_publish> ListE3()
        //{// 待考试卷
        //    ForExamDataContext exam = new ForExamDataContext();
        //    var getExamInfo = from e in exam.ex_publish where e.publish_endif=="N" && e.publish_endall=="N"
        //                      select e;
        //    return getExamInfo.ToList();
        //}
        //public IList<ex_publish> ListE4()
        //{// 待评试卷
        //    ForExamDataContext exam = new ForExamDataContext();
        //    var getExamInfo = from e in exam.ex_publish where e.publish_endif=="Y"&&e.publish_endall=="N"
        //                      select e;
        //    return getExamInfo.ToList();
        //}
        public string CheckDel(string name)
        {
            string flag ="no" ;
            
            ForExamDataContext all = new ForExamDataContext();
            var delc = from c in all.ex_publish
                       where c.publish_id == name
                       select c;
            foreach (var m in delc)
            {
                if (m.publish_endif == "否" && m.publish_endall == "否")
                {
                    flag = "yes";
                }
            }
            return flag;
        }
        public void DelExam(string name)
        {
            //
            ForExamDataContext all = new ForExamDataContext();
            var delp = from p in all.ex_paper_exam//删除关联的试卷表
                           where p.publish_id == name
                           select p;
            var delr = from r in all.ex_record  //删除关联的记录表
                          where r.publish_id == name
                          select r;
            var dela = from a in all.ex_answer
                       where a.publish_id == name //删除关联的答题表
                       select a;
            var dele = from e in all.ex_publish  //删除考试表中的信息
                       where e.publish_id == name
                       select e;
            foreach (var ep in delp)
            {
                all.ex_paper_exam.DeleteOnSubmit(ep);
            }
            foreach (var er in delr)
            {
                all.ex_record.DeleteOnSubmit(er);
            }
            foreach (var ea in dela)
            {
                all.ex_answer.DeleteOnSubmit(ea);
            }
            foreach (var ee in dele)
            {
                all.ex_publish.DeleteOnSubmit(ee);
            }
            all.SubmitChanges();
        }
        public string GetName(string name)
        {
            //ajax验证用户名
            string flag ="no";
            string word = "";
            ForExamDataContext user = new ForExamDataContext();
            var uname = from u in user.ex_user
                        
                        select u;
            foreach (var k in uname)
            {
                if (k.user_username==name)
                    flag = "yes";
            }
            if (name == "")
            {
                word = ("输入的用户名不可为空！");
            }
            else if (flag == "yes")
            {
                word = "不行，该用户名已被注册！";
            }
            else if (flag == "no")
            {
              
                word = "OK，该用户名可以使用！";
            }
            else
            {
                word = "数据库验证时出错了！";
            }
            return word;
        }
        public string Check(string name, string pwd)
        {
            ForExamDataContext check = new ForExamDataContext();
            string flag = "false";
            var user = from c in check.ex_user
                       select c;
            foreach (var m in user)
            {
                if (m.user_password == pwd && m.user_username == name)
                {
                    flag = "true";
                }
            }

            return flag;
        }
        public int? GetRole(string id)
        {
            ForExamDataContext role = new ForExamDataContext();
            int? back = 0;
            role.GetRoleByUser(id, ref back);
            return back;
        }
        public List<GetExamByUserResult> GetExamByUser(string name)
        {
            ForExamDataContext user = new ForExamDataContext();
            var ebu = user.GetExamByUser(name);
            return ebu.ToList();
        }
    }
}
